package com.beiding.fastmock.fram;

import org.springframework.stereotype.Component;

import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * @author 丁常磊
 * @date 2021/7/14 15:15
 */

@Component
public class ScriptExecutor {
    private ScriptEngine scriptEngine;

    {
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        scriptEngine = scriptEngineManager.getEngineByName("js");
    }

    public Object exe(String path, Bindings bindings) {
        try {
            return scriptEngine.eval("load('" + path + "')", bindings);
        } catch (ScriptException e) {
            throw new RuntimeException(e);
        }
    }

}
